var amapFile = require('../../../lib/map/amap-wx');
var markersData = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopAddress:[],
    markers: [],
    latitude: '',
    longitude: '',
    textData: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.latitude=="" || options.longitude==""){
      //调用定位信息
      //返回上一页
      wx.navigateBack({ changed: true });
    }else{
      this.setData({ 
        "latitude":options.latitude,
        "longitude":options.longitude
      })
    }

    if(options.shopAddress==undefined || options.shopAddress.length==0){
      //返回上一页
      wx.navigateBack({ changed: true });
    }else{
      const address = JSON.parse(options.shopAddress);
      this.setData({ 
        "shopAddress":address,
      })

      //加载地图
      var that = this;
      var myAmapFun = new amapFile.AMapWX({key:'b7fb20b92d40395ae9a70cb461e74cbc'});
      markersData = that.data.shopAddress;
      that.showMarkerInfo(markersData,0);
      that.changeMarkerColor(markersData,0);
    }

  },
  makertap: function(e) {
    var id = e.detail.markerId;
    var that = this;
    that.showMarkerInfo(markersData,id);
    that.changeMarkerColor(markersData,id);
  },
  showMarkerInfo: function(data,i){
    var that = this;
    that.setData({
      textData: {
        name: data[i].name,
        desc: data[i].address,
        latitude:data[i].latitude,
        longitude:data[i].longitude
      }
    });
  },
  changeMarkerColor: function(data,i){
    var that = this;
    var markers = [];
    for(var j = 0; j < data.length; j++){
      if(j==i){
        data[j].iconPath = "http://img.zaodianjian.top/image/common/selectedAdrs.png"; //如：..­/..­/img/marker_checked.png
      }else{
        data[j].iconPath = "http://img.zaodianjian.top/image/common/selectAdrs.png"; //如：..­/..­/img/marker.png
      }
      markers.push(data[j]);
    }
    that.setData({
      shopAddress: markers
    });
  },
  //选择取货地址
  checkme(){
      console.log(this.data.textData);
      var that = this;
      let name = that.data.textData.name;
      let toVal = that.data.shopAddress;
      for(let val in  toVal){
        if(toVal[val].name == name){
          wx.redirectTo({
            url: '/pages/shopping/checkout/checkout?addressId='+val,
          })
          break;
        }
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})